# photomanager.py
import os
import logging
from pysage import Actor, ActorManager, get_logger
from models.album import Album
import persistence
from persistence import dbtransaction

omanager = ActorManager.get_singleton()
logger = get_logger()

class MetaDataAPI(Actor):
    '''API that potentially parses the file system for picture folders and return metadata on demand'''
    def get_albumids(self):
        '''called by the broadcaster to virtual all albums, broadcast does not distinguish between folders or virtual folders'''
        return [x.id for x in self.get_albums()]
#    def get_pathids(self):
#        return [x.id for x in self.get_paths()]
    def get_albums(self):
        '''returns a list of albums'''
        return persistence.Album.query.all()
#    def get_paths(self):
#        return persistence.Path.query.all()
    def get_album_photos(self, albumid):
        '''list of ids'''
        return [x.id for x in persistence.Album.query().get(int(albumid)).images]
    def get_photo(self, photoid):
        return persistence.Image.query().get(photoid)
    def get_album(self, albumid):
        return persistence.Album.query().get(albumid)
    # authentication metadata
    def get_album_viewers(self, albumid):
        '''for temporary users: return 'username:password@clientid.yapss.com'
        otherwise, list of strings email addresses'''
        return persistence.Path.query().get(albumid).viewers
    def get_photo_users(self, photoid):
        '''for temporary users: return 'username:password@clientid.yapss.com'
        otherwise, list of strings email addresses'''
        return [x.name for x in persistence.User.query.all()]
    @dbtransaction
    def add_user(self, name):
        '''validate whether a user can be added, and add it'''
        user = persistence.User(name)
        return user
    def get_viewer_names(self):
        '''returns a list of usernames'''
        return [x.name for x in persistence.User.query.all()]
    def get_viewers(self):
        '''returns a list of user objects'''
        return persistence.User.query.all()
#    def get_path(self, pathid):
#        return persistence.Path.query().get(pathid)
#    @dbtransaction
#    def add_path(self, path):
#        '''validate whether a folder can be added, and add it'''
#        _path = persistence.Path(path)
#        return _path
    @dbtransaction
    def add_album(self, path):
        a = Album(path)
        return a
    @dbtransaction
    def remove_album(self, albumid):
        persistence.session.delete(self.get_album(albumid))
        
#    def get_registered_paths(self):
#        '''returns a list of registered concrete file system paths
#            this mehtod returns objects only'''
#        return [x for x in persistence.Path.query().all()]
    def edit_album_users(self, albumid, userids):
        '''associate only the userids with this album'''
        # note that a path is a path for which folders will be probed, folders are actually the entities that contain images
        # TODO: need to add the concept of folder
        persistence.Path.query().filter(persistence.Path.path == self.get_album_meta(albumid)['path']).one().set_viewers(userids)
        return True
#    @dbtransaction
#    def delete_all_albums(self):
#        for i in persistence.Album.query().all():
#            persistence.session.delete(i)
#        return True
#    @dbtransaction
#    def remove_path(self, pathid):
#        logger.info('removing path "%s"' % pathid)
#        persistence.session.delete(self.get_path(pathid))
        
        
        
        



